package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2022-10-30 17:26 星期日
 * description:
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper,HospitalSet> implements HospitalSetService {

    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public Map<String, Object> selectPage(Long page, Long limit) {
        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);
        Page<HospitalSet> selectPage = hospitalSetMapper.selectPage(hospitalSetPage, null);
        long total = selectPage.getTotal();
        List<HospitalSet> records = selectPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows",records);
        return map;
    }

    @Override
    public Map<String, Object> selectPageQuery(Long page, Long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        Page<HospitalSet> selectPage = null;
        if (hospitalSetQueryVo != null){
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();
            if (!StringUtils.isEmpty(hosname)){
                queryWrapper.like("hosname", hosname);
            }
            if (!StringUtils.isEmpty(hoscode)){
                queryWrapper.eq("hoscode", hoscode);
            }
            selectPage = hospitalSetMapper.selectPage(hospitalSetPage, queryWrapper);
        }else{
            selectPage = hospitalSetMapper.selectPage(hospitalSetPage, null);
        }
        List<HospitalSet> records = selectPage.getRecords();
        long total = selectPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("rows", records);
        map.put("total",total);
        return map;
    }


    /**
     * 获取签名信息
     * @param hoscode
     * @return
     */
    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = hospitalSetMapper.selectOne(queryWrapper);
        if (hospitalSet == null){
            throw new YyghException(20001,"该医院暂未开通权限");
        }
        String signKey = hospitalSet.getSignKey();
        return signKey;
    }

    @Override
    public HospitalSet getUrlByHoscode(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = hospitalSetMapper.selectOne(queryWrapper);
        return hospitalSet;
    }
}
